#pragma once
#include <HPSocket\HPSocket.h>
#include <rpcFw\byteBuffer.h>
#include<map>
#include<memory>
#include<rpcFw\Variant.h>
#include<rpcFw\global.h>

class DJsonCodec;

struct RPCFW_API PacketStruct{
	bool bCompleteHead;
	unsigned int bodySize;
	unsigned int jsonLength;
	CONNID dwConnID;
	Buffer buffer;
	Variant varData;

	PacketStruct(){
		bCompleteHead = false;
		bodySize = 0;
		jsonLength = 0;
	}
};

class RPCFW_API DServerInf :public CTcpServerListener
{
public:
	DServerInf();
	virtual void onRecvData(){};
	virtual ~DServerInf();

protected:
	EnHandleResult OnPrepareListen(ITcpServer* pSender, SOCKET soListen) override;
	EnHandleResult OnAccept(ITcpServer* pSender, CONNID dwConnID, UINT_PTR soClient) override;
	EnHandleResult OnHandShake(ITcpServer* pSender, CONNID dwConnID) override;
	EnHandleResult OnReceive(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)	 override;
	EnHandleResult OnSend(ITcpServer* pSender, CONNID dwConnID, const BYTE* pData, int iLength)	 override;
	EnHandleResult OnClose(ITcpServer* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override;
	EnHandleResult OnShutdown(ITcpServer* pSender)	 override;

private:
	void parsePacket(const char *,Variant &);

private:
	std::unique_ptr<DJsonCodec> m_pJsonCodec;
	std::map<int, PacketStruct> m_RecvPacket;
};

class RPCFW_API DClientInf :public CTcpClientListener
{
public:
	DClientInf();
	virtual void onRecvData() {};
	virtual ~DClientInf();

protected:
	EnHandleResult OnHandShake(ITcpClient* pSender, CONNID dwConnID) override;
	EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)	 override;
	EnHandleResult OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength)	 override;
	EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode) override;

private:
	std::unique_ptr<DJsonCodec> m_pJsonCodec;
	PacketStruct m_packetStruct;
};